home *** CD-ROM | disk | FTP | other *** search
- /* File "socket.h", Light Sockets - Copyright (C) Matt Slot, 1996 */
- /* Foundation layer for the Light Sockets networking abstraction routines. */
-
- #ifndef __SOCKET_HEADER__
- #define __SOCKET_HEADER__
-
- #ifndef __STD_TYPES_HEADER__
- #include "stdtypes.h"
- #endif /* __STD_TYPES_HEADER__ */
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* Preprocessor Defines */
-
- /* Primarily private defines */
- #define qGetSocketProtocol(st) (st & eSocketProtocolMask)
- #define qIsSocketTypeTCPIP(st) (qGetSocketProtocol(st) == eSocketProtocolTCPIP)
- #define qIsSocketTypeATalk(st) (qGetSocketProtocol(st) == eSocketProtocolATalk)
- #define qIsSocketTypeIPX(st) (qGetSocketProtocol(st) == eSocketProtocolIPX)
- #define qIsSocketTypeRPC(st) (qGetSocketProtocol(st) == eSocketProtocolRPC)
- #define qIsSocketTypeFile(st) (qGetSocketProtocol(st) == eSocketProtocolFile)
-
- #define qGetSocketTransfer(st) (st & eSocketTransferMask)
- #define qIsSocketTypeDatagram(st) \
- (qGetSocketTransfer(st) == eSocketTransferDatagram)
- #define qIsSocketTypeStream(st) \
- (qGetSocketTransfer(st) == eSocketTransferStream)
- #define qIsSocketTypeXaction(st) \
- (qGetSocketTransfer(st) == eSocketTransferXaction)
-
- /* Primarily public defines */
- #define qIsSocketTypeRawIP(st) \
- (((st & eSocketTypeMask) == eSocketTypeRawIP) ? true : false)
- #define qIsSocketTypeUDP(st) \
- (((st & eSocketTypeMask) == eSocketTypeUDP) ? true : false)
- #define qIsSocketTypeTCP(st) \
- (((st & eSocketTypeMask) == eSocketTypeTCP) ? true : false)
-
- #define qIsSocketTypeDDP(st) \
- (((st & eSocketTypeMask) == eSocketTypeDDP) ? true : false)
- #define qIsSocketTypeADSP(st) \
- (((st & eSocketTypeMask) == eSocketTypeADSP) ? true : false)
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- #define kSocketNoErrorStr "No Error"
- #define kSocketErrUnknownStr "Unknown Error"
- #define kSocketErrInternalStr "Internal Error"
- #define kSocketErrBadParamStr "Bad Parameter"
- #define kSocketErrOutOfMemStr "Out of Memory"
- #define kSocketErrNoSupportStr "Operation Not Supported"
- #define kSocketErrNoStackStr "Network Stack Missing"
- #define kSocketErrNotLoadedStr "Network Stack Not Loaded"
- #define kSocketErrCantLoadStr "Network Stack Couldn't Load"
- #define kSocketErrBadStackStr "Network Stack Invalid"
- #define kSocketErrCantBindStr "Can't Bind to Address"
- #define kSocketErrWasBoundStr "Socket already bound"
- #define kSocketErrFloodedStr "Socket Buffers Exhausted"
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- #undef AF_INET
- #define AF_INET 2
-
- #undef PF_UDP
- #define PF_UDP 17
-
- #undef PF_TCP
- #define PF_TCP 6
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- //#define kSocketLoggingSize 4096
- #define kSocketLoggingSize 2048
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* Enum/Structure/Class Definitions */
-
-
- typedef SInt32 SocketResult;
-
- enum {
- eSocketNoError = 0,
- eSocketErrBase = -7000,
-
- /* Private components */
- eSocketErrUnknown = eSocketErrBase - 0, /* Unknown, Unspecified */
- eSocketErrInternal = eSocketErrBase - 1, /* Private, Corruption */
- eSocketErrBadParam = eSocketErrBase - 2, /* Bad Parameter */
- eSocketErrOutOfMem = eSocketErrBase - 3, /* Out of Memory */
- eSocketErrNoSupport = eSocketErrBase - 4, /* Op not supported */
- eSocketErrNoStack = eSocketErrBase - 5, /* No network stack */
- eSocketErrNotLoaded = eSocketErrBase - 6, /* Stack not loaded */
- eSocketErrCantLoad = eSocketErrBase - 7, /* Can't load stack */
- eSocketErrBadStack = eSocketErrBase - 8, /* Network stack invalid */
- eSocketErrCantBind = eSocketErrBase - 9, /* Can't bind to address */
- eSocketErrWasBound = eSocketErrBase - 10, /* Socket already bound */
- eSocketErrFlooded = eSocketErrBase - 11 /* No more buffers */
- };
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- typedef SInt32 SocketType;
-
- enum {
- eSocketUninitialized = 0x00000000,
-
- eSocketProtocolTCPIP = 0x00000001,
- eSocketProtocolATalk = 0x00000002,
- eSocketProtocolIPX = 0x00000003,
- eSocketProtocolRPC = 0x00000004,
- eSocketProtocolFile = 0x00000005,
-
- eSocketTransferDatagram = 0x00000100,
- eSocketTransferStream = 0x00000200,
- eSocketTransferXaction = 0x00000300,
-
- eSocketTypeUDP = 0x00010101,
- eSocketTypeRawIP = 0x00020101,
- eSocketTypeTCP = 0x00010102,
- eSocketTypeDDP = 0x00010201,
- eSocketTypeADSP = 0x00010202,
-
- eSocketProtocolMask = 0x000000FF,
- eSocketTransferMask = 0x0000FF00,
- eSocketIdentifierMask = 0x00FF0000,
- eSocketReservedMask = 0xFF000000,
- eSocketTypeMask = 0x00FFFFFF
- };
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- typedef struct SocketAddress {
- UInt32 type;
- UInt32 length;
- union {
- Byte8 bytes[ARBITRARY_BUFFER_SIZE];
- struct {
- UInt16 type;
- UInt16 port;
- UInt32 host;
- Byte8 data[8];
- } tcpip;
- } buffer;
- } SocketAddress, *SocketAddressPtr;
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- typedef SocketResult (* SocketSpinProc)(class Socket *socket, void *refcon);
-
- typedef SocketResult (* DatagramWriteProc)(class DatagramSocket *socket,
- void *refcon, Byte8 *dataPtr, UInt32 dataLen, SocketAddressPtr address);
- typedef SocketResult (* DatagramReadProc)(class DatagramSocket *socket,
- void *refcon, Byte8 *dataPtr, UInt32 dataLen, SocketAddressPtr address);
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- typedef class Socket {
-
- public:
- virtual ~Socket (void);
-
-
- /* Basic member functions */
- virtual SocketResult Create (SocketType socketType);
- virtual SocketResult Tickle (void);
- virtual SocketResult Dispose (void);
-
- /* Primary member functions */
- SocketResult Bind (SocketAddressPtr reqAddress,
- SocketAddressPtr retAddress);
- SocketResult Unbind (void);
-
- SocketResult GetSpinProc (SocketSpinProc &proc,
- void * &refcon);
- SocketResult SetSpinProc (SocketSpinProc proc,
- void *refcon);
-
- /* Public accessor functions */
- SocketResult GetInstError (void);
- void ResetInstError (void);
-
- /* Network stack accessor functions */
- SocketType GetSocketType (void);
-
- Bool8 IsSocketInited (void);
- void SetSocketInited (Bool8 inited);
- Bool8 IsSocketBound (SocketAddressPtr boundTo);
- void SetSocketBound (Bool8 bound,
- SocketAddress &boundTo);
-
- #ifdef _DEBUG
- /* Packet logging functions */
- SocketResult EnableLogging (char *filename,
- Bool8 replacing=true);
- SocketResult DisableLogging (void);
- Bool8 IsLogging (void);
- #endif
-
- protected:
-
- /* Protect constructor - Base class can't be instantiated */
- Socket (void);
-
- /* Callback member functions */
- SocketResult CallSpinProc (void);
-
-
- /* Protected member data */
- SocketResult instError; /* Instance error storage */
- SocketType socketType; /* Internal socket information */
-
- Bool8 inited; /* Has been successfully inited */
- Bool8 bound; /* Has been successfully bound */
- SocketAddress boundTo; /* Has been successfully bound */
-
- class NetworkStack *stack; /* Underlying network interface */
-
- private:
-
- static SocketResult SpinStub (class Socket *socket,
- void *refcon);
- /* Callbacks */
- SocketSpinProc spinProc; /* Spin routine chain and data */
- void * spinRefcon; /* Spin routine chain and data */
-
- #ifdef _DEBUG
- /* Packet logging functions */
- Bool8 logEnabled;
- Bool8 logInited;
- Bool8 logReplace;
- Char8 logName[ARBITRARY_BUFFER_SIZE];
- Char8 *logBuffers[2];
- Char8 *curLogBuffer;
- SocketSpinProc logSpinProc;
- void * logSpinRefcon;
- DatagramReadProc logReadProc;
- void * logReadRefcon;
- DatagramWriteProc logWriteProc;
- void * logWriteRefcon;
-
- static SocketResult LoggingSpinStub (class Socket *socket,
- void *refcon);
- static SocketResult LoggingDatagramReadStub(class Socket *socket,
- void *refcon,
- Byte8 *dataPtr, UInt32 dataLen,
- SocketAddressPtr address);
- static SocketResult LoggingDatagramWriteStub(class Socket *socket,
- void *refcon,
- Byte8 *dataPtr, UInt32 dataLen,
- SocketAddressPtr address);
- void LoggingConvertBytes(Byte8 *dataPtr, UInt32 dataLen,
- Char8 *addr, Char8 *hex,
- Char8 *ascii);
- #endif
-
- } Socket, *SocketRef;
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* Function Prototypes */
-
-
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
- /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
-
- #endif /* __SOCKET_HEADER__ */
-
-